package cn.gok.manage.web;

import cn.gok.manage.service.ICartService;
import cn.gok.pojo.Cart;
import cn.gok.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;

/**
 * @author: 鸭鸭
 * @ClassName:CartController
 * @Package:cn.gok.manage.web
 * @Description:
 * @date: 2021/8/6
 * @time: 10:40
 */
@Controller
@RequestMapping("/cart")
public class CartController {
    @Autowired
    ICartService cartService;

    /*
     * 登录后查询指定用户购物车列表
     * */
    @RequestMapping(value = "/list.do", method = RequestMethod.GET)
    @ResponseBody
    public ResultData selectAllCartByUserId() {
        //登录后提供user_id
        int user_id = 1;

        return cartService.selectAllCartByUserId(user_id);
    }
    /*
     *登录后添加商品到购物车
     */

    @RequestMapping(value = "/add.do", method = RequestMethod.GET)
    @ResponseBody
    public ResultData addCart(@RequestParam("productId") int productId,
                              @RequestParam("count") int count) {

        //拦截器获取user_id
        int user_id = 26;
        System.out.println("Controller中的user_id" + user_id);
        return cartService.addCart(user_id, productId, count);
    }

    /*
     * 更新购物车某个产品数量
     * */
    @RequestMapping(value = "/update.do", method = RequestMethod.GET)
    @ResponseBody
    public ResultData updateCartCount(@RequestParam("productId") int productId,
                                      @RequestParam("count") int count) {
        int user_id = 26;
        return cartService.updateCartCountByProductId(user_id, productId, count);
    }

    /*
     * 移出购物车中某个或多个产品
     * */
    @RequestMapping(value = "/delete_product.do", method = RequestMethod.GET)
    @ResponseBody
    public ResultData deleteCartByProductIds(@RequestParam("productIds") List<Integer> list) {
        //拦截器获取用户Id
        int user_id = 26;
        return cartService.deleteCarts(list, user_id);
    }

    /*
    * 购物车选中某个商品
    * */
    @RequestMapping(value = "/select.do", method = RequestMethod.GET)
    @ResponseBody
    public ResultData selectCartChecked(@RequestParam("productId") int productId){
        //拦截器获取用户Id
        int userId = 26;
        return cartService.updateCheckedByProductId(userId,productId);
    }

    /*
    * 取消选中购物车某个商品
    * */
    @RequestMapping(value = "/un_select.do", method = RequestMethod.GET)
    @ResponseBody
    public ResultData UnSelectCartChecked(@RequestParam("productId") int productId){
        //拦截器获取用户Id
        int userId = 26;
        return cartService.updateNoCheackedByProductId(userId,productId);
    }

    /*
    * 查询在购物车里的产品数量
    * */
    @RequestMapping(value = "/get_cart_product_count.do", method = RequestMethod.GET)
    @ResponseBody
    public ResultData getCartProductCount(){
        //拦截器获取用户Id
        int userId = 26;
        return cartService.selectCountByUserId(userId);
    }

    /*
    * 全选购物车商品
    * */
    @RequestMapping(value = "/select_all.do", method = RequestMethod.GET)
    @ResponseBody
    public ResultData selectAllCheckedByUserId(){
        //拦截器获取用户Id
        int userId = 26;
        return cartService.updateAllCheckedByUserId(userId);
    }

    /*
    * 购物车取消全选
    * */
    @RequestMapping(value = "/un_select_all.do", method = RequestMethod.GET)
    @ResponseBody
    public ResultData selectUnAllCheckedByUserId(){
        //拦截器获取用户Id
        int userId = 26;
        return cartService.updateAllUnCheckedByUserId(userId);
    }







}



